package com.css.bjdt_core.report.service;

import java.math.BigDecimal;
import java.util.List;

import com.css.bjdt_core.cell.entity.Cell;
import com.css.bjdt_core.executive.report.ReportCache;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.report.entity.RptAreaTemporary;
import com.css.bjdt_core.util.CommonUtil;

@Service
public class RptAreaTemporaryService {
	@Autowired
	private Dao dao;

	private ReportCache cache = ReportCache.getInstance();

	//保存数据区域的值
	public void saveRptAreaTemporary(RptAreaTemporary rptAreaTemporary) {
		if (StringUtils.isBlank(rptAreaTemporary.getId())) {
			rptAreaTemporary.setId(CommonUtil.getUUID());
		}
		dao.insert(rptAreaTemporary);
	}

	//通过报表ID，获得起始行、列，终止行、列
	public RptAreaTemporary getRptAreaTemporary(String templateId) {
		RptAreaTemporary rptAreaTemporary = dao.fetch(RptAreaTemporary.class, Cnd.where("TEMPLATEID", "=", templateId));
		return rptAreaTemporary;
	}

	//删除根据报表模板ID，删除报表区域
	public void clearAreaTemporaryByTemplate(String templateId) {
		dao.clear(RptAreaTemporary.class, Cnd.where("TEMPLATEID", "=", templateId));
	}

	//根据报表ID获得当前报表的所有数据区域
	public List<RptAreaTemporary> queryRawRptAreaTemporaryList(String templateId) {
		return dao.query(RptAreaTemporary.class, Cnd.where("TEMPLATEID", "=", templateId));
	}

	//根据报表ID获得当前报表的所有数据区域
	public List<RptAreaTemporary> queryRptAreaTemporaryList(String templateId) {
		List<RptAreaTemporary> res = (List<RptAreaTemporary>) cache.get(ReportCache.AREA_CACHE, templateId);
		if(res == null) {
			res = dao.query(RptAreaTemporary.class, Cnd.where("TEMPLATEID", "=", templateId));
			cache.put(ReportCache.AREA_CACHE, templateId, res);
		}
		return res;
	}

	//算出这张表，是从第几行开始是表内区域的，也就是说有几行表头
	public Integer sumMinAbsStartRow(String templateId) {
		String sqlOracleStr = "select min(to_number(t.ABS_START_ROW)) as absStartRow from RPT_AREA_TEMPORARY t where t.templateid = @templateId";
		String sqlMySqlStr = "select min(CAST(t.abs_start_row AS SIGNED)) as absStartRow from rpt_area_temporary t where t.templateid = @templateId";
		Sql sql = Sqls.create(sqlOracleStr);
		sql.setCallback(Sqls.callback.integer());
		sql.params().set("templateId", templateId);
		dao.execute(sql);
		return new BigDecimal(sql.getObject(Integer.class)).subtract(new BigDecimal(1)).intValue();
	}

	//算出这张表，是从第几列开始是表内区域的，也就是说有几行表列
	public Integer sumMinAbsStartCol(String templateId) {
		String sqlOracleStr = "select min(to_number(t.ABS_START_COL)) as absStartCol from RPT_AREA_TEMPORARY t where t.templateid = @templateId";
		String sqlMySqlStr = "select min(CAST(t.ABS_START_COL AS SIGNED)) as absStartCol from rpt_area_temporary t where t.templateid = @templateId";
		Sql sql = Sqls.create(sqlOracleStr);
		sql.setCallback(Sqls.callback.integer());
		sql.params().set("templateId", templateId);
		dao.execute(sql);
		return new BigDecimal(sql.getObject(Integer.class)).subtract(new BigDecimal(1)).intValue();
	}
}
